package com.ugrow.Study_System.activity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;

import android.os.Build;
import android.os.SystemClock;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import com.ugrow.Study_System.R;

import java.util.Calendar;

public class DialogActivity extends AppCompatActivity {
    private int year;
    private int month;
    private int day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

    }


    public void normalDialog(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息");
        builder.setMessage("this is Message");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(DialogActivity.this, "确定1", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(DialogActivity.this, "取消1", Toast.LENGTH_SHORT).show();
            }
        });
        //点击其他区域,dialog不取消
        builder.setCancelable(false);
        builder.create().show();
    }

    //单选对话框
    public void aloneSelectDg(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择您的性别");
//        builder.setMessage("");
        //参数1:单选的内容数组;参数2:默认选中哪一项(-1默认都补选中);参数三:监听器
        final String[] itemNames = {"男", "女", "不详"};
        builder.setSingleChoiceItems(itemNames, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(DialogActivity.this, "您选择了:" + itemNames[i], Toast.LENGTH_SHORT).show();
                dialogInterface.dismiss();
            }
        });
        builder.create().show();

    }

    //多选对话框
    public void moreSelectDg(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("兴趣爱好");

        final String[] nameStr = {"运动", "游戏", "旅游", "睡觉"};
        final boolean[] checkedItem = {false, true, true, false};
        final boolean[] checkedItemFinall = checkedItem;
        builder.setMultiChoiceItems(nameStr, checkedItem, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                checkedItemFinall[i] = b;
            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                for (int j = 0; j < checkedItem.length; j++) {
                    checkedItem[j] = checkedItemFinall[j];
                }
                StringBuffer sb = new StringBuffer();
                for (boolean a : checkedItem) {
                    sb.append(String.valueOf(a) + " ");

                }
                Toast.makeText(DialogActivity.this, sb, Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                StringBuffer sb = new StringBuffer();
                for (boolean a : checkedItem) {
                    sb.append(String.valueOf(a) + " ");

                }
                Toast.makeText(DialogActivity.this, sb, Toast.LENGTH_SHORT).show();
            }
        });

        builder.create().show();

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void dateSelectDg(View view) {
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(DialogActivity.this,
                        "" + year + "-" + (month+1) + "-" + dayOfMonth
                        , Toast.LENGTH_SHORT).show();
            }
        }, calendar.get(Calendar.YEAR)
                , calendar.get(Calendar.MONTH)
                , calendar.get(Calendar.DAY_OF_MONTH))
                .show();


    }
    //时间选择器
    public void timeSelectDg(View view) {
//        TimePicker tp = new TimePicker();
        Calendar calendar = Calendar.getInstance();
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(DialogActivity.this
                        , ""+hourOfDay+"时"+minute+"分"
                        , Toast.LENGTH_SHORT).show();
            }
        },calendar.get(Calendar.HOUR_OF_DAY)
        ,calendar.get(Calendar.MINUTE)
        ,false).show();


    }

    public void progressBarDg(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("下载进度");
        progressDialog.setMessage("loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0;i<=100;i++) {
                    SystemClock.sleep(50);
                    progressDialog.setProgress(i);
                }
                progressDialog.dismiss();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DialogActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }).start();
        progressDialog.show();

    }


}
